#include "imagemanager.h"
#include <QDebug>
#include <QDir>


ImageManager::ImageManager()
{

}


ImageManager::~ImageManager(){
    while(!m_images.empty()){
        QMap<QString,AED_Image*>::iterator it;
        it = m_images.begin();
        delete it.value();
        m_images.remove(it.key());
    }
}

AED_Image* ImageManager::LoadImageK(const QString &path){
    AED_Image *img = new AED_Image();

    if (m_images.contains(path)){

        return 0;
    }
    if(!img->Load(path)){
        delete img;
        return 0;
    }

    m_images[path] = img;
    return img;

}

AED_Image* ImageManager::LoadImageK(const QString &idpath, const QString &realpath){
    AED_Image *img = new AED_Image();

    if (m_images.contains(idpath)){

        return 0;
    }
    if(!img->Load(realpath)){
        delete img;
        return 0;
    }

    m_images[idpath] = img;
    return img;

}


AED_Image* ImageManager::GetImage(const QString &path){
    if(!m_images.contains(path)){
        return 0;
    }

    return m_images.value(path);
}

AED_Image* ImageManager::GetImage(int pos){
    int i=0;
    QMap<QString,AED_Image*>::iterator it= m_images.begin();

    while(i<pos && it!=m_images.end()){
        i++;
        it++;
    }
    if(it==m_images.end()){
        return 0;
    }

    return it.value();
}

void ImageManager::EraseImage(const QString &name){
    m_images.remove(name);
}

void ImageManager::Clean(){
    QMap<QString,AED_Image*>::iterator it= m_images.begin();

    while(!m_images.empty()){
        delete it.value();
        it = m_images.erase(it);
    }

}

QStringList ImageManager::GetImageNames(){
    QStringList list;
    QMap<QString,AED_Image*>::iterator it ;
    for(it= m_images.begin();it!=m_images.end();++it){
        list.append(it.value()->GetName());
    }
    return list;

}

QStringList ImageManager::GetImagePaths(){
    QStringList list;
    QMap<QString,AED_Image*>::iterator it ;
    for(it= m_images.begin();it!=m_images.end();++it){
        list.append(it.value()->GetPath());
    }
    return list;

}

void ImageManager::Save(QTextStream &file){
    QMap<QString,AED_Image*>::iterator it ;
    for(it= m_images.begin();it!=m_images.end();++it){
        it.value()->Save(file);
    }
}

void ImageManager::Save(QDomDocument &doc, QDomElement &header, const QString &scene_path){
    QMap<QString,AED_Image*>::iterator it ;
    for(it= m_images.begin();it!=m_images.end();++it){
        it.value()->Save(doc,header,scene_path);
    }
}

bool ImageManager::Load(QDomElement &header, QString &scene_path){
    QDir dir(scene_path);


    QDomNodeList images = header.elementsByTagName("image");
    for(int i=0;i<images.count();i++){
        QDomElement ielem = images.at(i).toElement();
        QString path = dir.absoluteFilePath(ielem.attribute("path"));

        AED_Image *img = LoadImageK(ielem.attribute("path"),path);
        if(!img)
            return false;

        img->Load(ielem);


    }

    return true;
}



void ImageManager::UpdatePaths(const QString &path){
    QDir dir(path);


    QMap<QString,AED_Image*>::iterator it ;
    for(it= m_images.begin();it!=m_images.end();++it){
        it.value()->SetPath(dir.relativeFilePath(it.value()->GetPath()));
    }
}
